package com.stageadminserver.adminserver.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.stageadminserver.adminserver.service.UserService;
import com.stagecommon.common.pojo.RespBean;
import com.stagecommon.common.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yxl
 * @since 2021-11-09
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 根据用户id查询用户
     * @param user
     * @return
     */
    @PostMapping("/selectUser")
    public RespBean<User> selectUser(@RequestBody User user) {
        return userService.selectUser(user);
    }

    /**
     * 分页查询用户
     * @param currentPage
     * @param size
     * @param user
     * @return
     */
    @PostMapping("/selectAllUser")
    public RespBean<IPage<User>> selectAllUser(@RequestParam(defaultValue = "1") Integer currentPage,
                                               @RequestParam(defaultValue = "10") Integer size,
                                               User user) {
        return userService.getAllUserByPage(user, currentPage, size);
    }

    /**
     * 根据用户id删除用户
     * @param user
     * @return
     */
    @PostMapping("/deleteUserById")
    public RespBean deleteUserById(@RequestBody User user) {
        return userService.deleteUserById(user);
    }

    /**
     * 根据用户id修改用户信息
     * @param user
     * @return
     */
    @PostMapping("/changeUserById")
    public RespBean changeUserById(@RequestBody User user) {
        return userService.changeUserById(user);
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public RespBean addUser(@RequestBody User user) {
        return userService.addUser(user);
    }

    /**
     * 查询所有用户列表
     * @return
     */
    @GetMapping("/getAll")
    public RespBean<List<User>> getAll() {
        return RespBean.operationSuccess("ok", 200, userService.list());
    }
}
